/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bombercloud.client;

import bombercloud.commons.BlocoFixo;
import bombercloud.commons.BlocoQuebra;
import bombercloud.commons.Boneco;
import bombercloud.commons.Mapa;
import bombercloud.commons.Response;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

/**
 *
 * @author Delano
 */
///professor, o senhor disse que essa thread seria inutilizada...que agora seria usado BomberCloudClientServer
public class ThreadClient extends Thread {

    Bomber refBomber;
    BomberSegundo refBomberSegundo;
    BomberCloudUI refUI;
    InterfaceGanhador refInterfaceGanhador;
    ObjectInputStream inputStream;
    ObjectOutputStream outputStream;
    ArrayList<Boneco> listBonecos;
    ArrayList<BlocoQuebra> listDLugaresPossiveis;
    ArrayList<BlocoQuebra> listDLugaresNLivres;
    private ArrayList<BlocoFixo> listParedes;
    Socket socket;
    private Boneco boneco;
    public boolean executando = true;

    public ThreadClient(Bomber refC, Socket socket1, ObjectInputStream inputStreamC, ObjectOutputStream objectOutputStream1, BomberCloudUI refUIC) {
        refBomber = refC;
        socket = socket1;
        inputStream = inputStreamC;
        outputStream = objectOutputStream1;
        refUI = refUIC;
    }

    @Override
    public void run() {
        while (executando) {
            try {
                Response response;
                response = (Response) inputStream.readObject();

                if (response.getType().toString().equalsIgnoreCase("MOVIMENT")) {
                    boneco = (Boneco) response.getResponseObject2();
                    refUI.bonecoMovimento(boneco);
                }
                if (response.getType().toString().equalsIgnoreCase("DELETE_BOMBA")) {
                    BlocoQuebra bq = (BlocoQuebra) response.getResponseObject2();
                    refUI.explosao(bq);
                    Mapa mapa = (Mapa) response.getResponseObject();
                    refUI.atualizacaoMapa(mapa);
                }
                if (response.getType().toString().equalsIgnoreCase("DELETE_FOGO")) {
                    ArrayList<BlocoQuebra> lf = (ArrayList<BlocoQuebra>) response.getResponseObject2();
                    refUI.removeFogo(lf);
                    Mapa mapa = (Mapa) response.getResponseObject();
                    refUI.atualizacaoMapa(mapa);
                }
                if (response.getType().toString().equalsIgnoreCase("DELETE_BLOCOS")) {
                    Mapa mapa = (Mapa) response.getResponseObject();
                    refUI.atualizacaoMapa(mapa);
                }

                if (response.getType().toString().equalsIgnoreCase("DELETE_BONECO")) {
                    System.out.println("respondeu: Respondeu_TYPE_DELETE_BONECO");
                    Mapa mapa = (Mapa) response.getResponseObject();
                    listBonecos = (ArrayList<Boneco>) response.getResponseObject2();
                    refUI.atualizacaoMapa(mapa);
                    refUI.mudaFotoPlacar(listBonecos);
                }
                if (response.getType().toString().equalsIgnoreCase("QTD_BONECOS")) {
                    int testa = response.getQtdBoneco();
                    switch (testa) {
                        case 0: {
                            refUI.finalizaPartida(0, this);
                            break;
                        }
                        case 1: {
                            refUI.finalizaPartida(1, this);
                            break;
                        }
                    }
                }

                if (response.getType().toString().equalsIgnoreCase("CREATE_BOMBA")) {
                    Mapa mapa = (Mapa) response.getResponseObject();
                    refUI.atualizacaoMapa(mapa);
                }
                if (response.getType().toString().equalsIgnoreCase("START")) {
                    Mapa mapa = (Mapa) response.getResponseObject();
                    if (refBomber != null) {
                        refBomber.iniciarPartida(refBomber, this, mapa);
                    } else {
                        refBomberSegundo.iniciarPartida(refBomberSegundo, this, mapa);
                    }
                }
                if (response.getType().toString().equalsIgnoreCase("FINALIZA")) {
                    refInterfaceGanhador.finalizaPartida(refInterfaceGanhador, this);
                }

                if (response.getType().toString().equalsIgnoreCase("PLAYERS")) {
                    listBonecos = (ArrayList<Boneco>) response.getResponseObject();
                    if (refUI != null) {
                        refUI.setListBonecos(listBonecos);
                    }
                    for (Boneco b : listBonecos) {
                        if (refBomber != null) {
                            refBomber.atualizaBotoesEscolhido(b.getNumero());
                        } else {
                            refBomberSegundo.atualizaBotoesEscolhido(b.getNumero());
                        }

                    }
                }
                if (response.getType().toString().equalsIgnoreCase("FIXED_BLOCKS")) {
                    listParedes = (ArrayList<BlocoFixo>) response.getResponseObject();
                    refUI.setListParedes(listParedes);
                }
                if (response.getType().toString().equalsIgnoreCase("BROKE_BLOCKS")) {
                    listDLugaresNLivres = (ArrayList<BlocoQuebra>) response.getResponseObject();
                    refUI.setListDLugaresNLivres(listDLugaresNLivres);
                }
                if (response.getType().toString().equalsIgnoreCase("CREATE_PLAYER")) {
                    Boneco boneco = (Boneco) response.getResponseObject();
                    listBonecos = (ArrayList<Boneco>) response.getResponseObject2();
                    if (response.isResponseObjectPai()) {

                        if (refBomber != null) {
                            refBomber.setBoneco(boneco);
                        } else {
                            refBomberSegundo.setBoneco(boneco);
                        }


                        for (Boneco b : listBonecos) {
                            if (b.getNumero() != boneco.getNumero()) {
                                if (refBomber != null) {
                                    refBomber.atualizaBotoesEscolhido(b.getNumero());
                                } else {
                                    refBomberSegundo.atualizaBotoesEscolhido(b.getNumero());
                                }

                            }
                        }
                    } else {
                        if (refBomber != null) {
                            refBomber.atualizaBotoesEscolhido(boneco.getNumero());
                        } else {
                            refBomberSegundo.atualizaBotoesEscolhido(boneco.getNumero());
                        }

                    }
                    if (listBonecos.size() >= 2) {
                        if (refBomber != null) {
                            refBomber.atualizaBotaoIniciar();
                        } else {
                            refBomberSegundo.atualizaBotaoIniciar();
                        }

                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
                //Logger.getLogger(ThreadClient.class.getName()).log(Level.SEVERE, null, ex);
                break;
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
                //Logger.getLogger(ThreadClient.class.getName()).log(Level.SEVERE, null, ex);
                break;
            }
        }
        if (socket != null && !socket.isClosed()) {
            try {
                socket.close();
            } catch (IOException ex) {
            } finally {
                socket = null;
            }

        }
    }

    public Bomber getRef() {
        return refBomber;
    }

    public void setRef(Bomber ref) {
        this.refBomber = ref;
    }

    public ObjectInputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(ObjectInputStream inputStream) {
        this.inputStream = inputStream;
    }

    public BomberCloudUI getRefUI() {
        return refUI;
    }

    public void setRefUI(BomberCloudUI refUI) {
        this.refUI = refUI;
    }

    public ObjectOutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(ObjectOutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public InterfaceGanhador getRefIG() {
        return refInterfaceGanhador;
    }

    public void setRefIG(InterfaceGanhador refIG) {
        this.refInterfaceGanhador = refIG;
    }

    public Bomber getRefBomber() {
        return refBomber;
    }

    public void setRefBomber(Bomber refBomber) {
        this.refBomber = refBomber;
    }

    public BomberSegundo getRefBomberSegundo() {
        return refBomberSegundo;
    }

    public void setRefBomberSegundo(BomberSegundo refBomberSegundo) {
        this.refBomberSegundo = refBomberSegundo;
    }
}
